<?php
/**
 * Users controller class
 *
 */

class PrivilegesController extends Admin_Controller
{
    //private $table_tags;

	public function init()
    {
    	parent::init();
    	$this->_table = new Admin_Table_Privileges();
    	$this->setActiveCat(764);
    }

    /**
     * index action method
     *
     */
    public function indexAction()
    {
    	$this->page_type = 'list';

    	$reset = $this->_request->getParam('reset', '');
    	$popular = $this->_request->getParam('popular', 0);

    	$form = new Llib_Form($this->_request);
    	$data = array();

    	if ($this->_request->isPost())
    	{
	    	$data['is_active'] = $this->_request->getParam('is_active', -1);
	    	$data['is_top'] = $this->_request->getParam('is_top', -1);
    	}

        $params = $this->SessionVars($data, array('is_active', 'is_top'), $reset);

        $params['type'] = 0;

    	$lclm = new Llib_List($this->_request, $this->_sess);
    	$lclm->AddParams(array('checkboxes' => true));
    	$lclm->AddColumn('Заголовок', 'title_ru', array('width' => '90%', 'sort' => 1));

		$lclm->AddFunc(array('type' => 'order'));
    	$lclm->AddFunc(array('title' => 'Главная', 'name' => 'top', 'field' => 'is_top'));
    	$lclm->AddFunc();

    	$lclm->AddData($this->_table->GetDataList($lclm->page, $lclm->s, $lclm->pager_count, $params));

        $data['is_active'] = $form->GetActiveList(@$params['is_active']);
        $data['is_top'] = $form->GetTopList(@$params['is_top']);
        $form->AddData($data);

        $this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->assign('List', $lclm->GetList());
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

    public function editAction()
    {
    	$id = $this->_getParam('id');
    	$saveexit = $this->_getParam('saveexit');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$data = $this->_table->GetDataRow($id);

    	$form = new Llib_Form($this->_request);

        if ($this->_request->isPost())
        {
			$d = $form->GetPostedData(array('tags'));

        	if($d['idt'] == '') $d['idt'] = $this->tstring($d['title_ru']);

			if($this->_table->update($d, $id, $this->_user->id))
			{
			   if(is_null($saveexit))  $form->SetSuccess();
			   else $this->_redirect('/'.$this->controller);
			}
        	else $form->SetError($this->_table->GetError());
        }

        $data = $this->_table->GetDataRow($id);


        $form->AddData($data);

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->xmlWriter->writeAttribute('id', $id);
	        $this->view->assign($form->GetFrom());
	        //$this->view->assign('photos', $this->table_objects->GetDataParamsList(array('id_article' => $id)));
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

	// *****************************
    // PHOTO

    public function photosAction()
    {
    	$id = $this->_request->getParam('id');
    	$saveexit = $this->_getParam('saveexit');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$form = new Llib_Form($this->_request);

    	if ($this->_request->isPost())
    	{
    		$d = $form->GetPostedData();
    		$this->table_objects->order_dragsort($d['list1SortOrder']);
    		if(!is_null($saveexit))$this->_redirect('/'.$this->controller);
    	}

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->xmlWriter->writeAttribute('id', $id);
	        $this->view->xmlWriter->writeAttribute('table_name', 'objects');
			$this->view->assign($form->GetFrom());
	        $this->view->assign('photos', $this->table_objects->GetDataParamsList(array('id_article' => $id)));
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

    public function addphotoAction()
    {
    	$article_id = $this->_request->getParam('id');
    	if(!($article_id > 0)) $this->_redirect('/error/e404');

    	$d['id_article'] = $article_id;

		$id = $this->table_objects->insert($d, $this->_user->id);

		if($id > 0)
		{
			$this->_redirect('/'.$this->controller.'/editphoto/id/'.$id);
		}else $this->_redirect('/error/e404');
    }

	public function editphotoAction()
    {
    	$id = $this->_request->getParam('id');
    	$saveexit = $this->_getParam('saveexit');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$data = $this->table_objects->GetDataRow($id);
    	$art = $this->_table->GetDataRow($data['id_article']);

    	$form = new Llib_Form($this->_request);

        if ($this->_request->isPost())
        {
			$d = $form->GetPostedData();

			$this->LoadAvatar($id, $form, $d, 'objects');

        	if($this->table_objects->update($d, $id, $this->_user->id)) $form->SetSuccess();
        	else $form->SetError($this->_table->GetError());

			if(!is_null($saveexit))  $this->_redirect('/'.$this->controller.'/photos/'.$art['id']);
        }

        $data = $this->table_objects->GetDataRow($id);

        $form->AddData($data);

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->xmlWriter->writeAttribute('id', $art['id']);
	        $this->view->xmlWriter->writeAttribute('title', $art['title']);
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

public function ajaxOrderAction()
    {
    	$id = $this->_request->getParam('id');
    	$norder = $this->_request->getParam('norder');
    	$old = $this->_request->getParam('old');
    	if(!($id > 0)) die('ERROR');

    	$data = $this->_table->GetDataRow($id);

    	$this->_table->OrderNumber($id, $norder, $old);

    	die('OK');
    }

    public function orderAction()
    {
    	$id = $this->_request->getParam('id');
    	$d = $this->_request->getParam('d');
    	if(!($id > 0)) $this->_redirect('/error/e404');
    	if(!($d > 0)) $this->_redirect('/'.$this->controller);

    	$data = $this->_table->GetDataRow($id);

    	$this->_table->OrderDirect($id, $d);
    	$this->_redirect('/'.$this->controller);

    }

    public function reorderAction()
    {
    	//$id = $this->_request->getParam('id');
    	//if(!($id > 0)) $this->_redirect('/error/e404');

        	$this->_table->Reorder();
        	$this->_redirect('/'.$this->controller);

    }

}
